<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use DB;

class SearchController extends Controller
{
    public function index(Request $request ,$id)
    {

    	// dd($request->all());
        //保存搜索条件
        $where = [];
        // 查询数据库
    	
        //判断是否有搜索条件
        if($request->has('title')){
            //获取搜索的条件
            $title = $request->input('title');
            // dd($title);
            //添加到将要携带到分页中的数组中 
            $where['title']=$title;
            //给查询添加where条件
           $sp = DB::table('detail')->where('title','like',"%{$title}%")->get();
        }else{
        	$sp = DB::table('detail')->get();
        }


    	$fl = DB::table('type')->where('upid',0)->get();
        foreach ($fl as $k => $v) {
           $v->f = DB::table('type')->where('upid',$v->id)->get();
           foreach($v->f as $key=>$val){
            $val->h = DB::table('type')->where('upid',$val->id)->get();
           }
        }
        // dd($sp);
        // dd($fl);
        $ms = DB::table('flash')->get();

        return view('home.search',[ 'sp'=>$sp ,'fl'=>$fl ,'id'=>$id ,'where'=>$where ,'ms'=>$ms]);
    }

    	public function create(Request $request ,$id)
    	{
    		$fl = DB::table('type')->where('upid',0)->get();
        foreach ($fl as $k => $v) {
           $v->f = DB::table('type')->where('upid',$v->id)->get();
           foreach($v->f as $key=>$val){
            $val->h = DB::table('type')->where('upid',$val->id)->get();
           }
        }
        $ms = DB::table('flash')->get();

        // dd($request->all());
        //保存搜索条件
        $where = [];
        // 查询数据库
        
        //判断是否有搜索条件
        if($request->has('title')){
            //获取搜索的条件
            $title = $request->input('title');
            // dd($title);
            //添加到将要携带到分页中的数组中 
            $where['title']=$title;
            //给查询添加where条件
           $sp = DB::table('detail')->where('title','like',"%{$title}%",'cid',$id)->get();
        }else{
            $sp = DB::table('detail')->where('cid',$id)->get();
        }

        
        // dd($ms);
        // dd($sp);
    		return view('home.search', ['$id'=>$id ,'fl'=>$fl ,'sp'=>$sp ,'ms' => $ms]);
    	} 

}
